package com.qf.contract.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.common.base.exception.ControllerException;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.contract.entity.FContractApproval;
import com.qf.contract.qo.ContractApprovalQo;
import com.qf.contract.service.ContractApprovalService;
import com.qf.contract.service.FContractApprovalService;
import com.qf.contract.vo.ContractApprovalVo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "合同审批")
@RestController
@RequestMapping("/contractapproval")
public class ContractApprovalController {
    @Resource
    ContractApprovalService contractApprovalService;
    @Resource
    FContractApprovalService fContractApprovalService;

    @ApiOperation("合同待审批查询")
    @GetMapping("/conditionalQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractName", value = "合同名称"),
            @ApiImplicitParam(name = "contractNumber", value = "合同编号"),
            @ApiImplicitParam(name = "crocessTitle", value = "流程标题"),
            @ApiImplicitParam(name = "workArrivalTime", value = "工作到达时间"),
            @ApiImplicitParam(name = "typeOfContract", value = "合同类型 1采购合同,2总包合同"),
            @ApiImplicitParam(name = "current", value = "页码"),
            @ApiImplicitParam(name = "size", value = "页数")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult conditionalQuery(ContractApprovalQo contractApprovalQo, @RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        try {
            IPage<ContractApprovalVo> contractApprovalVoIPage = contractApprovalService.conditionalQuery(contractApprovalQo, current, size, 0);
            System.out.println(contractApprovalVoIPage);
            return RespResult.success(contractApprovalVoIPage);
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


    @ApiOperation("合同已审批查询")
    @GetMapping("/contractapprovedquery")
    @ApiImplicitParams({
            @ApiImplicitParam(name =
                    "current", value = "页码"),
            @ApiImplicitParam(name = "size", value = "页数")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult ContractApprovedQuery(ContractApprovalQo contractApprovalQo, @RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "10") int size) {
        try {
            return RespResult.success(contractApprovalService.ContractApprovedQuery(contractApprovalQo, current, size));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("合同审批流程查询")
    @GetMapping("/contractapprovalview")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同id")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult contractapprovalview(int id) {
        try {
            return RespResult.success(fContractApprovalService.contractApprovalView(id));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


    @ApiOperation("合同审批详情查询")
    @GetMapping("/contractapprovalviewdetails")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同id")
    })
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult contractApprovalViewDetails(int id) {
        try {
            return RespResult.success(fContractApprovalService.contractApprovalViewDetails(id));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }

    @ApiOperation("领导审批接口")
    @PutMapping("/leaderapproval")
    @ApiResponses({@ApiResponse(code = 200, message = "成功")})
    public RespResult leaderApproval(FContractApproval fContractApproval) {
        try {
            return RespResult.success(fContractApprovalService.leaderApproval(fContractApproval));
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        } catch (Exception e) {
            throw new ControllerException(ResultCode.SYS_ERROR);
        }
    }


}
